package com.hh.admin.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.hh.admin.entity.SysMenuRole;
import com.hh.admin.entity.SysRole;
import com.hh.admin.mapper.SysMenuMapper;
import com.hh.admin.mapper.SysMenuRoleMapper;
import com.hh.admin.mapper.SysRoleMapper;
import com.hh.admin.service.ISysRoleService;
import com.hh.core.entity.ZtreeEntity;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hxn
 * @since 2018-01-16
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {

	@Autowired
	SysMenuRoleMapper sysMenuRoleMapper;
	
	@Autowired
	SysMenuMapper sysMenuMapper;
	
	@Override
	public boolean addRoleMenu(Long id, String menuIds) {
		Map<String, Object> columnMap = new HashMap<String, Object>();
		columnMap.put("ROLE_ID", id);
		sysMenuRoleMapper.deleteByMap(columnMap);
		
		String[] menuId = menuIds.split(",");
		SysMenuRole sysMenuRole = new SysMenuRole();
		for(String str:menuId){
			sysMenuRole.setMenuId(Long.parseLong(str));
			sysMenuRole.setRoleId(id);
			sysMenuRoleMapper.insert(sysMenuRole);
		}
		return true;
	}

	@Override
	public Map<String,Object> getTreeById(Long id) {
		Map<String,Object> map = new HashMap<>();
		List<ZtreeEntity> ztreeList = sysMenuMapper.getMenuTreeByRoleId(id);
		map.put("ztree", ztreeList);
		StringBuilder sb = new StringBuilder();
		for(ZtreeEntity ze:ztreeList){
			if(ze.getChecked()){
				sb.append(ze.getId()+",");
			}
		}
		if(sb.length()>0){
			map.put("menuIds", sb.substring(0, sb.length()-1));
		}
		return map;
	}
	
}
